//公司职位
import React, { Component } from 'react';
import {
    View,
    Text,
} from 'react-native';

var Dimensions = require('Dimensions');
var height = Dimensions.get('window').height;

import Companyone from './every/companyone';
import Companytwo from './every/companytwo';
import Loading from '../../../util/loading/loading'
import { SelCompanyInfoAction } from '../../../actions/postionAction';      //加载action
import { InsertFollowCompanyAction, DeleteFollowBykeyMob } from '../../../actions/postionAction';      //加载action
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

class Company extends Component {
    // 初始化模拟数据
    constructor(props) {            //构造器只执行一次
        super(props);
        this.state = {
            isloading: true
        }
    };

    //加载后渲染页面
    componentDidMount() {
        this.props.actions.SelCompanyInfoAction({ companyId: this.props.navigation.state.params.useId }); //初始化查询
    }

    componentDidUpdate() {
        if (!this.props.xzCompany.isloading && this.state.isloading) {
            this.setState({ isloading: false })
        }
    }

    render() {
        const { xzCompany } = this.props.xzCompany.data;
        if (this.state.isloading) {
            return (
                // <View style={{
                //     marginVertical: 10, height: 100, alignItems: 'center', justifyContent: 'flex-end'
                // }}>
                //     <Text>玩命加载ing</Text>
                // </View>
                <Loading/>
                );
        } else {
            //else if ret判断错误情况
            return (
                <View style={{ flex: 1, backgroundColor: '#eeeeee' }}>
                    <Companyone
                        xzCompany={xzCompany}
                        actions={this.props.actions}
                        followState={this.props.xzCompany.data.followState}
                        navigation={this.props.navigation}
                    />
                    <Companytwo xzCompany={xzCompany} navigation={this.props.navigation} />
                </View>
            )
        }
    }

}

//组合action
var actions = {
    SelCompanyInfoAction,
    InsertFollowCompanyAction,
    DeleteFollowBykeyMob
}

//连接state到props
const mapStateToProps = state => {
    return {
        xzCompany: state.SelCompanyInfoReducer             //自己的reducers
    }
}

//连接action
const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}

//导出组合好的组件(组合后状态改变重新渲染组合组件的页面)
export default connect(
    mapStateToProps, mapDispatchToProps
)(Company);